﻿/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
STRUCT(LoginClass , GuiObjs)
//{
   static void OK(LoginClass &login)
   {
      if(!ValidName    (login.getTextLine("name")()))Gui.msgBox("Error", "Invalid User Name.\nIt must be between 1-32 characters,\nIt may not start with a space or .\nIt may not contain following characters: \\ / : * ? \" < > | @");else
      if(!ValidPassword(login.getTextLine("pass")()))Gui.msgBox("Error", "Invalid Password.\nIt must be between 3-16 characters.");else
      {
         Account=login.getTextLine("name")();
         ClientSendLogin(Server, login.getTextLine("name")(), login.getTextLine("pass")());
      }
   }

   STRUCT(TextLine , ::TextLine)
   //{
      virtual void update(C GuiPC &gpc)
      {
         super::update(gpc);
         if(LoginClass *lc=(LoginClass*)user)
         {
            if(Gui.kb()==this && Kb.bp(KB_ENTER)){Kb.eat(KB_ENTER); OK(*lc);}
         }
      }
   };

   void create()
   {
      replaceTextLine<TextLine>(); load("gui/login.gobj"); Gui+=T;
      getTextLine("name").activate().user=this;
      getTextLine("pass").user=this;
      getButton  ("ok"  ).func(OK, T);
   }
}LoginWindow;
/******************************************************************************/
STRUCT(RegisterClass , GuiObjs)
//{
   static void OK(RegisterClass& reg)
   {
      if(!ValidName    (reg.getTextLine("name")()                                ))Gui.msgBox("Error", "Invalid User Name.\nIt must be between 1-32 characters,\nIt may not start with a space or .\nIt may not contain following characters: \\ / : * ? \" < > | @");else
      if(!ValidPassword(reg.getTextLine("pass")()                                ))Gui.msgBox("Error", "Invalid Password.\nIt must be between 3-16 characters.");else
      if(!Equal        (reg.getTextLine("pass")(), reg.getTextLine("pass_conf")()))Gui.msgBox("Error", "Password does not match Password Confirmation.");else
      {
         ClientSendRegister(Server, reg.getTextLine("name")(), reg.getTextLine("pass")(), reg.getTextLine("email")());
      }
   }

   STRUCT(TextLine , ::TextLine)
   //{
      virtual void update(C GuiPC &gpc)
      {
         super::update(gpc);
         if(RegisterClass *reg=(RegisterClass*)user)
         {
            if(Gui.kb()==this && Kb.bp(KB_ENTER)){Kb.eat(KB_ENTER); OK(*reg);}
         }
      }
   };

   STRUCT(Window , ::Window)
   //{
      virtual void update(C GuiPC &gpc)
      {
         super::update(gpc);
         if(Gui.window()==this && Kb.bp(KB_ESC)){Kb.eat(KB_ESC); fadeOut();}
      }
   };

   void create()
   {
      replaceTextLine<TextLine>(); replaceWindow<Window>(); load("gui/register.gobj"); Gui+=T; hide(); getWindow("").button[2].show();
      getTextLine("name"     ).user=this;
      getTextLine("pass"     ).user=this;
      getTextLine("pass_conf").user=this;
      getTextLine("email"    ).user=this;
      getButton  ("register" ).func(OK, T);
   }

   void activate()
   {
      show();
      getTextLine("name"     ).set(LoginWindow.getTextLine("name")());
      getTextLine("pass"     ).set(LoginWindow.getTextLine("pass")());
      getTextLine("pass_conf").clear().activate();
   }
}RegisterWindow;
/******************************************************************************/
static void SelectServer(Ptr)
{
   StateSelectServer.set(0.2f);
}
Button SelectServerButton;
/******************************************************************************/
Bool InitLogin()
{
               LoginWindow.create();
            RegisterWindow.create();
   Gui+=SelectServerButton.create(Rect_RD(D.w()-0.1f, -D.h()+0.1f, 0.3, 0.05), "Select Server").func(SelectServer);
   return true;
}
void ShutLogin()
{
          LoginWindow.del();
       RegisterWindow.del();
   SelectServerButton.del();
}
/******************************************************************************/
Bool UpdateLogin()
{
   Gui.update();

   // check server state
   if(Server.state()!=CONNECT_GREETED){Gui.msgBox("Error", "Connection Lost"); StateSelectServer.set(0.2f);}

   // check if any data received from server
   if(Server.receive(0))switch(Server.data.getByte())
   {
      case CS_LOGIN:
      {
         LOGIN_RESULT result; ClientRecvLogin(Server.data, result);
         switch(result)
         {
            case LOGIN_INVALID_USER_NAME         : Gui.msgBox("Error", "Invalid User Name Format"); break;
            case LOGIN_INVALID_PASSWORD          : Gui.msgBox("Error", "Invalid Password Format" ); break;
            case LOGIN_USER_DOES_NOT_EXIST       : RegisterWindow.activate(); Gui.msgBox("", S+"User \""+LoginWindow.getTextLine("name")()+"\" not found.\nPlease register."); break;
            case LOGIN_USER_OK_INCORRECT_PASSWORD: Gui.msgBox("Error", "Incorrect password"); break;
            case LOGIN_USER_ALREADY_LOGGED       : Gui.msgBox("Error", "User already logged in"); break;
            case LOGIN_USER_OK_PASSWORD_OK       : StateSelectChr.set(0.2f); break;
         }
      }break;

      case CS_REGISTER:
      {
         RegisterWindow.hide    ();
            LoginWindow.activate();
         Bool ok; ClientRecvRegister(Server.data, ok); if(ok)Gui.msgBox("", "Account Created Successfully.");else Gui.msgBox("Error", "Error Creating Account");
      }break;
   }

   return true;
}
void DrawLogin()
{
   D.clear(BLACK);
   Gui.draw();
}
/******************************************************************************/
State StateLogin(UpdateLogin, DrawLogin, InitLogin, ShutLogin);
/******************************************************************************/
